from PyQt5.QtWidgets import (
    QWidget, QVBoxLayout, QLabel, 
    QLineEdit, QPushButton, QMessageBox,
    QHBoxLayout, QFrame
)
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtCore import Qt

class LoginWindow(QWidget):
    def __init__(self, auth_client):
        super().__init__()
        self.auth_client = auth_client
        self.setup_ui()
         # 设置窗口属性
        self.setWindowTitle("智能分析平台 - 用户登录")  # ← 主系统名+功能
        self.setWindowIcon(QIcon("assets/logo.png"))  # ← 添加品牌图标
        self.resize(400, 350)
        # 设置窗口标志，保留最小化和关闭按钮
        self.setWindowFlags(Qt.Window | Qt.WindowMinimizeButtonHint | Qt.WindowCloseButtonHint)
        
    def setup_ui(self):
        # 主布局
        main_layout = QVBoxLayout()
        main_layout.setContentsMargins(30, 30, 30, 30)
        main_layout.setSpacing(20)
        
        # 添加应用图标/标题
        self.lbl_title = QLabel("欢迎登录")
        self.lbl_title.setAlignment(Qt.AlignCenter)
        self.lbl_title.setStyleSheet("""
            QLabel {
                font-size: 24px;
                font-weight: bold;
                color: #2c3e50;
                margin-bottom: 20px;
            }
        """)
        
        # 登录表单容器
        form_frame = QFrame()
        form_frame.setStyleSheet("""
            QFrame {
                background-color: #ffffff;
                border-radius: 8px;
                padding: 20px;
            }
        """)
        
        form_layout = QVBoxLayout(form_frame)
        form_layout.setSpacing(15)
        
        # 用户名输入
        username_layout = QVBoxLayout()
        self.lbl_username = QLabel("用户名:")
        self.lbl_username.setStyleSheet("font-size: 14px; color: #34495e;")
        
        self.txt_username = QLineEdit()
        self.txt_username.setPlaceholderText("请输入用户名")
        self.txt_username.setStyleSheet("""
            QLineEdit {
                padding: 10px;
                border: 1px solid #bdc3c7;
                border-radius: 4px;
                font-size: 14px;
            }
            QLineEdit:focus {
                border: 1px solid #3498db;
            }
        """)
        
        username_layout.addWidget(self.lbl_username)
        username_layout.addWidget(self.txt_username)
        
        # 密码输入
        password_layout = QVBoxLayout()
        self.lbl_password = QLabel("密码:")
        self.lbl_password.setStyleSheet("font-size: 14px; color: #34495e;")
        
        self.txt_password = QLineEdit()
        self.txt_password.setPlaceholderText("请输入密码")
        self.txt_password.setEchoMode(QLineEdit.Password)
        self.txt_password.setStyleSheet("""
            QLineEdit {
                padding: 10px;
                border: 1px solid #bdc3c7;
                border-radius: 4px;
                font-size: 14px;
            }
            QLineEdit:focus {
                border: 1px solid #3498db;
            }
        """)
        
        password_layout.addWidget(self.lbl_password)
        password_layout.addWidget(self.txt_password)
        
        # 登录按钮
        self.btn_login = QPushButton("登 录")
        self.btn_login.setStyleSheet("""
            QPushButton {
                background-color: #3498db;
                color: white;
                border: none;
                padding: 12px;
                border-radius: 4px;
                font-size: 16px;
                font-weight: bold;
            }
            QPushButton:hover {
                background-color: #2980b9;
            }
            QPushButton:pressed {
                background-color: #1c6ea4;
            }
            QPushButton:disabled {
                background-color: #95a5a6;
            }
        """)
        self.btn_login.setCursor(Qt.PointingHandCursor)
        
        # 底部链接
        bottom_layout = QHBoxLayout()
        bottom_layout.setContentsMargins(0, 10, 0, 0)
        
        self.btn_forgot = QPushButton("忘记密码?")
        self.btn_forgot.setStyleSheet("""
            QPushButton {
                color: #3498db;
                border: none;
                text-decoration: underline;
                font-size: 12px;
                padding: 0;
            }
        """)
        self.btn_forgot.setCursor(Qt.PointingHandCursor)
        
        self.btn_register = QPushButton("注册账号")
        self.btn_register.setStyleSheet("""
            QPushButton {
                color: #3498db;
                border: none;
                text-decoration: underline;
                font-size: 12px;
                padding: 0;
            }
        """)
        self.btn_register.setCursor(Qt.PointingHandCursor)
        
        bottom_layout.addWidget(self.btn_forgot)
        bottom_layout.addStretch()
        bottom_layout.addWidget(self.btn_register)
        
        # 组装表单
        form_layout.addLayout(username_layout)
        form_layout.addLayout(password_layout)
        form_layout.addWidget(self.btn_login)
        form_layout.addLayout(bottom_layout)
        
        # 添加到主布局
        main_layout.addWidget(self.lbl_title)
        main_layout.addWidget(form_frame)
        main_layout.addStretch()
        
        self.setLayout(main_layout)
        
        # 窗口样式
        self.setStyleSheet("""
            QWidget {
                background-color: #f5f7fa;
                font-family: "Microsoft YaHei";
            }
        """)
        
        # 连接信号
        self.btn_login.clicked.connect(self.handle_login)
        self.auth_client.login_success.connect(self.on_login_success)
        self.auth_client.login_failed.connect(self.on_login_failed)
    
    def handle_login(self):
        username = self.txt_username.text().strip()
        password = self.txt_password.text().strip()
        
        if not username:
            self.show_error("用户名不能为空")
            self.txt_username.setFocus()
            return
        
        if not password:
            self.show_error("密码不能为空")
            self.txt_password.setFocus()
            return
        
        self.btn_login.setEnabled(False)
        self.btn_login.setText("登录中...")
        self.auth_client.login(username, password)
    
    def on_login_success(self, user_data):
        self.btn_login.setEnabled(True)
        self.btn_login.setText("登 录")
        self.close()
        
    def on_login_failed(self, error_msg):
        self.btn_login.setEnabled(True)
        self.btn_login.setText("登 录")
        self.show_error(error_msg)
    
    def show_error(self, message):
        # 修改：使用更友好的错误提示框
        msg = QMessageBox(self)
        msg.setIcon(QMessageBox.Critical)
        msg.setWindowTitle("登录错误")
        msg.setText(message)
        
        # 设置样式
        msg.setStyleSheet("""
            QMessageBox {
                font-family: "Microsoft YaHei";
                font-size: 14px;
            }
            QMessageBox QLabel {
                color: #e74c3c;
            }
        """)
        msg.exec_()
        # QMessageBox.critical(self, "错误", message)